Package Question2_4

Source Code of Question2_4.QuestionB

package Question2_4;

import CtCILibrary.LinkedListNode;

public class QuestionB {

  public static LinkedListNode partition(LinkedListNode node, int x) {
    LinkedListNode beforeStart = null;
    LinkedListNode afterStart = null;
   
    /* Partition list */
    while (node != null) {
      LinkedListNode next = node.next;
      if (node.data < x) {
        /* Insert node into start of before list */
        node.next = beforeStart;
        beforeStart = node; 
      } else {
        /* Insert node into front of after list */
        node.next = afterStart;
        afterStart = node;
     
      node = next;
    }
   
    /* Merge before list and after list */
    if (beforeStart == null) {
      return afterStart;
    }
   
    LinkedListNode head = beforeStart;
    while (beforeStart.next != null) {
      beforeStart = beforeStart.next;
    }
    beforeStart.next = afterStart;
    return head;
  }
 
  public static void main(String[] args) {
    int length = 20;
    LinkedListNode[] nodes = new LinkedListNode[length];
    for (int i = 0; i < length; i++) {
      nodes[i] = new LinkedListNode(i >= length / 2 ? length - i - 1 : i, null, null);
    }
   
    for (int i = 0; i < length; i++) {
      if (i < length - 1) {
        nodes[i].setNext(nodes[i + 1]);
      }
      if (i > 0) {
        nodes[i].setPrevious(nodes[i - 1]);
      }
    }
   
    LinkedListNode head = nodes[0];
    System.out.println(head.printForward());
   
    LinkedListNode h = partition(head, 7);
    System.out.println(h.printForward());
  }

}
TOP

Related Classes of Question2_4.QuestionB

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.